﻿using System;
using System.Collections.Generic;
using System.Text;
using Cemit.PolyProto;
using Google.Protobuf;

namespace Cemit.PolyServer
{
    /// <summary>
    /// 使用一个ID注册到连接所在的服务器
    /// </summary>
    class LoginHandle : IMessageHandle<Msg_Net_Login>
    {
        public void Execute(ClientState clientState, IMessage message)
        {
            Msg_Net_Login msg = message as Msg_Net_Login;

            Player player = new Player(clientState, msg.Id);

            if (clientState.server.Players.Contains(player))
            {
                //ID冲突
                clientState.Send(new Msg_Net_LoginResult
                {
                    Result = LoginResult.IdConflict
                });
            }
            else
            {
                clientState.server.Players.AddPlayer(player);
                //ID冲突
                clientState.Send(new Msg_Net_LoginResult
                {
                    Result = LoginResult.Successful
                });
            }

            this.Log("玩家已登录：" + msg.Id);
        }
    }
}
